<template>
  <div id="app">
    <div class="app-container">
      <!-- 顶部导航栏 -->
      <header class="app-header">
        <div class="header-content">
          <div class="logo-section">
            <div class="logo-wrapper">
              <svg width="32" height="32" viewBox="0 0 32 32" class="logo-icon">
                <path d="M16 2L2 10v12l14 8 14-8V10L16 2zm0 4.5l10.5 6-10.5 6-10.5-6L16 6.5zm-10.5 8L16 20.5l10.5-6v11L16 28 5.5 22.5v-11z" fill="currentColor"/>
                <circle cx="16" cy="15" r="3" fill="#409EFF"/>
              </svg>
            </div>
            <div class="logo-text">
              <h1 class="app-title">API Studio</h1>
              <p class="app-subtitle">一体化接口开发平台</p>
            </div>
          </div>
          
          <div class="header-actions">
            <el-tag type="success" effect="dark" class="version-tag">
              v1.0
            </el-tag>
          </div>
        </div>
      </header>
      
      <!-- 主要内容区域 -->
      <main class="app-main">
        <div class="main-content">
          <!-- 模板选择区域 -->
          <section class="template-section">
            <div class="section-header">
              <h2 class="section-title">
                <el-icon class="title-icon"><Collection /></el-icon>
                场景模板
              </h2>
              <p class="section-description">选择预设模板快速开始</p>
            </div>
            
            <div class="template-grid">
              <div 
                v-for="(template, index) in apiTemplates" 
                :key="template.id"
                class="template-card"
                @click="applyTemplate(template.id)"
                :style="{ animationDelay: `${index * 0.1}s` }"
              >
                <div class="template-icon">
                  <el-icon size="24">
                    <component :is="getTemplateIcon(template.id)" />
                  </el-icon>
                </div>
                <div class="template-content">
                  <h3 class="template-name">{{ template.name }}</h3>
                  <p class="template-desc">{{ template.description }}</p>
                </div>
              </div>
            </div>
          </section>
          
          <!-- 编辑器和调试器区域 -->
          <div class="editor-debugger-container">
            <div class="editor-container">
              <ApiDocEditor v-model="apiData" @update:modelValue="handleApiDataChange" />
            </div>
            <div class="debugger-container">
              <ApiDebugger :api-doc="apiData" />
            </div>
          </div>
        </div>
      </main>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { 
  Collection, 
  User, 
  List, 
  Upload 
} from '@element-plus/icons-vue'
import ApiDocEditor from './components/ApiDocEditor.vue'
import ApiDebugger from './components/ApiDebugger.vue'
import { apiTemplates, getTemplateById } from './utils/apiTemplates'

// 为模板添加描述信息和图标
const enhancedTemplates = [
  {
    id: 'login',
    name: '登录接口',
    icon: 'User',
    description: '用户登录认证接口，包含用户名密码参数'
  },
  {
    id: 'paginatedList',
    name: '分页列表',
    icon: 'List',
    description: '通用分页数据列表接口，支持分页和搜索'
  },
  {
    id: 'fileUpload',
    name: '文件上传',
    icon: 'Upload',
    description: '文件上传接口，支持文件和描述信息'
  }
]

export default {
  name: 'App',
  components: {
    ApiDocEditor,
    ApiDebugger,
    Collection,
    User,
    List,
    Upload
  },
  setup() {
    // API数据
    const apiData = ref({
      method: 'GET',
      url: '/api/example',
      parameters: [],
      response: []
    })
    
    // 获取模板图标
    const getTemplateIcon = (templateId) => {
      const template = enhancedTemplates.find(t => t.id === templateId)
      return template ? template.icon : 'Collection'
    }
    
    // 应用模板
    const applyTemplate = (templateId) => {
      const template = getTemplateById(templateId)
      if (template) {
        apiData.value = template
      }
    }
    
    // 处理API数据变更
    const handleApiDataChange = (newApiData) => {
      apiData.value = newApiData
    }
    
    return {
      apiData,
      apiTemplates: enhancedTemplates,
      getTemplateIcon,
      applyTemplate,
      handleApiDataChange
    }
  }
}
</script>

<style>
#app {
  height: 100vh;
  font-family: 'Helvetica Neue', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin: 0;
  padding: 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  overflow: hidden;
}

.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* 顶部导航栏样式 */
.app-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  padding: 0 24px;
  height: 64px;
  display: flex;
  align-items: center;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 1600px;
  margin: 0 auto;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-wrapper {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.logo-wrapper:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.05);
}

.logo-icon {
  color: white;
}

.logo-text .app-title {
  font-size: 22px;
  font-weight: 700;
  margin: 0;
  color: white;
  letter-spacing: 0.5px;
}

.logo-text .app-subtitle {
  font-size: 12px;
  margin: 2px 0 0 0;
  color: rgba(255, 255, 255, 0.8);
}

.version-tag {
  background: rgba(64, 158, 255, 0.3);
  border: 1px solid rgba(64, 158, 255, 0.5);
}

/* 主要内容区域 */
.app-main {
  flex: 1;
  overflow: hidden;
  padding: 20px;
}

.main-content {
  max-width: 1600px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 模板区域 */
.template-section {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.section-header {
  margin-bottom: 20px;
}

.section-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #303133;
}

.title-icon {
  color: #409EFF;
}

.section-description {
  font-size: 14px;
  color: #909399;
  margin: 0;
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 16px;
}

.template-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid #ebeef5;
  display: flex;
  align-items: flex-start;
  gap: 16px;
  animation: fadeInUp 0.6s ease-out forwards;
  opacity: 0;
  transform: translateY(20px);
}

.template-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
  border-color: #409EFF;
}

.template-icon {
  background: #ecf5ff;
  color: #409EFF;
  width: 48px;
  height: 48px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.template-content {
  flex: 1;
}

.template-name {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 4px 0;
  color: #303133;
}

.template-desc {
  font-size: 13px;
  color: #909399;
  margin: 0;
  line-height: 1.5;
}

/* 编辑器和调试器容器 */
.editor-debugger-container {
  display: flex;
  gap: 20px;
  flex: 1;
  min-height: 0;
}

.editor-container {
  flex: 1.4;
  min-width: 0;
}

.debugger-container {
  flex: 1;
  min-width: 0;
}

/* 动画定义 */
@keyframes fadeInUp {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 1024px) {
  .editor-debugger-container {
    flex-direction: column;
  }
  
  .editor-container,
  .debugger-container {
    flex: 1;
  }
  
  .template-grid {
    grid-template-columns: 1fr;
  }
}
</style>